<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');
// load base class if needed
require_once( APPPATH . 'controllers/base/OperatorBase.php' );

class barangsupply extends ApplicationBase {

    // constructor
    public function __construct() {
        parent::__construct();
        // load model
        $this->load->model('m_barangsupply');
        $this->load->model('m_milik');
        // load library
        $this->load->library('tnotification');
        $this->load->library('pagination');
        // load helpers
    }

    // welcome administrator
    public function index() {
        // set template content
        $this->smarty->assign("template_content", "master/barangsupply/list.html");

        $search = $this->tsession->userdata('master_barangsupply_search');
        $this->smarty->assign("search", $search);
        $barangsupply_cd = empty($search['barangsupply_cd']) ? '%' : '%' . $search['barangsupply_cd'] . '%';
        $barangsupply_name = empty($search['barangsupply_name']) ? '%' : '%' . $search['barangsupply_name'] . '%';

        /* start of pagination --------------------- */
        // pagination
        $config['base_url'] = site_url("master/barangsupply/index/");
        $config['total_rows'] = $this->m_barangsupply->get_count_all_barangsupply_search(array($barangsupply_cd, $barangsupply_name));
        $config['uri_segment'] = 4;
        $config['per_page'] = 20;
        $this->pagination->initialize($config);
        $pagination['data'] = $this->pagination->create_links();
        // pagination attribute
        $start = $this->uri->segment(4, 0) + 1;
        $end = $this->uri->segment(4, 0) + $config['per_page'];
        $end = (($end > $config['total_rows']) ? $config['total_rows'] : $end);
        $pagination['start'] = ($config['total_rows'] == 0) ? 0 : $start;
        $pagination['end'] = $end;
        $pagination['total'] = $config['total_rows'];
        // pagination assign value
        $this->smarty->assign("pagination", $pagination);
        $this->smarty->assign("no", $start);
        /* end of pagination ---------------------- */
        // get data
        $params = array($barangsupply_cd, $barangsupply_name, ($start - 1), $config['per_page']);
        $this->smarty->assign("rs_id", $this->m_barangsupply->get_all_barangsupply_pagination_search($params));
        // notification
        $this->tnotification->display_notification();
        $this->tnotification->display_last_field();

        //data
        $rs_milik = $this->m_milik->get_all_milik();
        $this->smarty->assign("rs_milik", $rs_milik);

        // output
        parent::display();
    }

    public function proses_cari() {
        // set page rules
        $this->_set_page_rule("R");
        // data
        if ($this->input->post('save') == "Reset") {
            $this->tsession->unset_userdata('master_barangsupply_search');
        } else {
            $params = array(
                "barangsupply_cd" => $this->input->post("barangsupply_cd"),
                "barangsupply_name" => $this->input->post("barangsupply_name")
            );
            $this->tsession->set_userdata("master_barangsupply_search", $params);
        }
        // redirect
        redirect("master/barangsupply");
    }

    public function add() {
        // $this->_set_page_rule("R");
        // set template content
        $this->smarty->assign("template_content", "master/barangsupply/add.html");

        // notification
        $this->tnotification->display_notification();
        $this->tnotification->display_last_field();
        // output
        parent::display();
    }

    // add process
    public function add_process() {
        // cek input 
        $this->tnotification->set_rules('barangsupply_cd', 'Kode', 'trim|required');
        $this->tnotification->set_rules('barangsupply_name', 'Nama', 'trim|required|max_length[100]');

        //set parameter
        $params = array(
            $this->input->post('barangsupply_cd'),
            $this->input->post('barangsupply_name'),
            $this->input->post('barangsupply_keterangan'),
            $this->com_user['user_id']
        );

        $check = $this->m_barangsupply->get_count_all_barangsupply_by_cd($params);

        // process
        if ($this->tnotification->run() !== FALSE) {
            // insert
            if ($check > 0) {
                $this->tnotification->sent_notification("error", "Kode data sudah tersedia");
            } else if ($this->m_barangsupply->insert($params)) {
                // notification
                $this->tnotification->delete_last_field();
                $this->tnotification->sent_notification("success", "Data berhasil disimpan");
            } else {
                // default error
                $this->tnotification->sent_notification("error", "Data gagal disimpan");
            }
        } else {
            // default error
            $this->tnotification->sent_notification("error", "Data gagal disimpan");
        }
        // default redirect
        redirect("master/barangsupply/add");
    }

    public function edit($barangsupply_cd) {
        // $this->_set_page_rule("R");
        // set template content
        $this->smarty->assign("template_content", "master/barangsupply/edit.html");

        //data
        $rs_detail = $this->m_barangsupply->get_barangsupply_by_barangsupply_cd($barangsupply_cd);
        $this->smarty->assign("rs_detail", $rs_detail);


        // notification
        $this->tnotification->display_notification();
        $this->tnotification->display_last_field();
        // output
        parent::display();
    }

    public function edit_process() {
        // cek input 
        $this->tnotification->set_rules('barangsupply_cd', 'Kode', 'trim|required');
        $this->tnotification->set_rules('barangsupply_name', 'Nama', 'trim|required|max_length[100]');

        //cek data
        $check = $this->m_barangsupply->get_count_all_barangsupply_by_cd($this->input->post('barangsupply_cd'));

        // process
        if ($this->tnotification->run() !== FALSE) {
            // insert
            if ($this->input->post('barangsupply_cd') == $this->input->post('barangsupply_cd_old')) {
                // parameter            
                $params = array(
                    $this->input->post('barangsupply_name'),
                    $this->input->post('barangsupply_keterangan'),
                    $this->com_user['user_id'],
                    $this->input->post('barangsupply_cd')
                );

                if ($this->m_barangsupply->edit($params)) {
                    // notification
                    $this->tnotification->delete_last_field();
                    $this->tnotification->sent_notification("success", "Data berhasil disimpan");
                } else {
                    // default error
                    $this->tnotification->sent_notification("error", "Data gagal disimpan");
                }
            } else {
                if ($check > 0) {                    
                    $this->tnotification->sent_notification("error", "Kode data sudah tersedia");
                    redirect("master/barangsupply/edit/" . $this->input->post('barangsupply_cd_old'));
                    
                } else {
                    //delete
                    $this->m_barangsupply->delete($this->input->post('barangsupply_cd_old'));

                    //insert
                    $params = array(
                        $this->input->post('barangsupply_cd'),
                        $this->input->post('barangsupply_name'),
                        $this->input->post('barangsupply_keterangan'),
                        $this->com_user['user_id']
                    );
                    $this->m_barangsupply->insert($params);

                    $this->tnotification->sent_notification("success", "Data berhasil disimpan");
                }
            }
        } else {
            // default error
            $this->tnotification->sent_notification("error", "Data gagal disimpan");
        }
        // default redirect
        redirect("master/barangsupply/edit/" . $this->input->post('barangsupply_cd'));
    }

    public function delete($barangsupply_cd) {
        // $this->_set_page_rule("R");
        // set template content
        $this->smarty->assign("template_content", "master/barangsupply/delete.html");
        //data
        $result = $this->m_barangsupply->get_barangsupply_by_barangsupply_cd($barangsupply_cd);
        $this->smarty->assign("result", $result);
        // notification
        $this->tnotification->display_notification();
        $this->tnotification->display_last_field();
        // output
        parent::display();
    }

    public function delete_process() {
        // cek input 
        $this->tnotification->set_rules('barangsupply_cd', 'Kode', 'trim|required');
        // process
        if ($this->tnotification->run() !== FALSE) {
            // parameter          
            // insert
            if ($this->m_barangsupply->delete($this->input->post('barangsupply_cd'))) {
                // notification
                $this->tnotification->delete_last_field();
                $this->tnotification->sent_notification("success", "Data berhasil dihapus");
            } else {
                // default error
                $this->tnotification->sent_notification("error", "Data gagal dihapus");
            }
        } else {
            // default error
            $this->tnotification->sent_notification("error", "Data gagal dihapus");
        }
        // default redirect
        redirect("master/barangsupply/");
    }

}
